package com.portal.incurridos.persistence.seguridad;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.portal.incurridos.persistence.utilities.ApplicationConstants;
import com.portal.incurridos.persistence.utilities.Configuracion;
/**
 * @author Alan Dávila
 * e-mail ustarga@gmail.com
 * date Jul 5, 2013 7:52:14 PM
 */
public class Administrator implements Filter {

    /* (non-Javadoc)
     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
     */
    public final void init(final FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        Configuracion configuracion = Configuracion.newInstance();
        String url = httpRequest.getRequestURI().toString();
        configuracion.setIdioma(request.getLocale());
        HttpSession session = httpRequest.getSession();
        Object usuario = session.getAttribute(ApplicationConstants.SESSION_USUARIO);
//        if (url.contains(ApplicationConstants.URL_NOSESSION) ||
//                url.contains(ApplicationConstants.URL_LOGIN) ||
//                url.contains(ApplicationConstants.URL_REGISTRO) ||
//                url.contains(ApplicationConstants.URL_EXISTE) ||
//                url.contains(ApplicationConstants.URL_PAISES) ||
//                url.contains(ApplicationConstants.URL_PREGUNTAS)) {
//            chain.doFilter(request, response);
//            return;
//        }
        if (usuario == null) {
            HttpServletResponse httpResponse = (HttpServletResponse) response;
//            httpResponse.sendRedirect(ApplicationConstants.URL_NOSESSION);
            return;
        }
        chain.doFilter(request, response);
        return;
    }

    /* (non-Javadoc)
     * @see javax.servlet.Filter#destroy()
     */
    public void destroy() {

    }

}
